const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

Page({
  data: {
    bannerInfo: {
      'imgUrl': '',
      'name': ''
    },
    categoryFilter: false,
    filterCategory: [],
    goodsList: [],
    categoryId: 0,
    currentSortType: 'default',
    currentSort: 'add_time',
    currentSortOrder: 'desc',
    page: 1,
    size: 100,

    total: '0',
    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },
    startNum: 1, //页数

    curisOnSale: '',

    data: {
      isOnSale: 1,
      isHot: 1,
      orderById: 4,
      goodsName: '', // 可选  商品名
    },

    listnum: 10, //分页列表个数
  },


  //上拉获取更多列表
  hotActivityLoadMore: function (e) {
    var that = this
    var search = that.data.search
    if (search) {
      that.listPage(that.data.inputVal)
    } else if (!search) {
      that.listPage('')
    }

  },

  //分页查询列表
  listPage: function (goodsName) {
    var that = this;
    //判断是否还有下一页
    if (that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })

      var num = that.data.listnum
      var pageNum = that.data.startNum
      var url = 'current=' + pageNum + '&size=' + num
      var data = that.data.data
      data.goodsName = goodsName

      util.requestNoToken(api.HotGoodList + url, data, "POST").then(function (res) {
        wx.hideLoading();
        if (res.success) {
          var listSize = res.obj.records.length;
          var newData = that.data.myAgentList.list;
          var lis = res.obj.records;
          lis.forEach(function (item, index, array) {
            array[index] = {
              id: item.id,
              primaryPicUrl: item.primaryPicUrl,
              goodsName: item.goodsName,
              marketPrice: item.marketPrice,
              retailPrice: item.retailPrice,
              categoryName: item.categoryName,
              sellVolume: item.sellVolume,
              goodsBrief: item.goodsBrief
            }
          });
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度等于于0表示当前没有数据，停止
          if (listSize == 0) {
            newList.hasMore = false;
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            return;
            //当长度小于10表示没有下页数据了
          } else if (listSize < num) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          that.setData({
            myAgentList: newList
          });

          that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          console.log("获取全部商品列表失败")
        }
      }).catch(function (res) {
        wx.hideLoading();
        console.log("获取数据失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  //获取商品列表首页数据
  getGoodsList: function (data) {
    var that = this
    var num = that.data.listnum
    var pageNum = that.data.startNum
    var url = 'current=' + pageNum + '&size=' + num;
    wx.showLoading({
      title: '加载中',
    })
    util.requestNoToken(api.HotGoodList + url, data, "POST").then(function (res) {
      if (res.success) {
        wx.hideLoading();
        var listSize = res.obj.records.length;
        var newData = that.data.myAgentList.list;
        var lis = res.obj.records;
        lis.forEach(function (item, index, array) {
          array[index] = {
            id: item.id,
            primaryPicUrl: item.primaryPicUrl,
            goodsName: item.goodsName,
            marketPrice: item.marketPrice,
            retailPrice: item.retailPrice,
            categoryName: item.categoryName,
            sellVolume: item.sellVolume,
            goodsBrief: item.goodsBrief
          }
        });
        newData = lis;
        var total1 = res.obj.total;
        var newList = {};
        //当长度小于5表示没有下页数据了
        if (listSize < num) {
          newList.hasMore = false;
        } else {
          newList.hasMore = true;
        }
        newList.list = newData;
        newList.listSize = listSize;
        that.setData({
          myAgentList: newList,
          total: total1
        });
        that.setData({
          startNum: 2
        })
      } else {
        wx.hideLoading();
        console.log("获取全部商品列表失败")
      }
    }).catch(function (res) {
      wx.hideLoading();
      console.log("获取数据失败！")
    })
  },

  //初始化分页数据
  getNewData: function () {
    var that = this

    var data = {
      isOnSale: 1,
      isHot: 1,
      orderById: 4,
      goodsName: '',
    }

    var myAgentList = {
      hasMore: true,
      list: [],
      listSize: 0,
    }
    that.setData({
      myAgentList: myAgentList,
      startNum: 1,
      data: data
    })
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    var data = that.data.data
    that.getNewData();
    // 获取综合列表
    that.getGoodsList(data);
  },


  //列表筛选过滤
  openSortFilter: function (event) {
    var that = this
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        that.getFrsCate();
        that.setData({
          categoryFilter: !that.data.categoryFilter,
          currentSortType: 'category',
          currentSort: 'add_time',
          currentSortOrder: 'desc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        let num = 5;
        if (that.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
          num = 6;
        }
        that.setData({
          currentSortType: 'price',
          currentSortOrder: tmpSortOrder,
          categoryFilter: false
        });
        if (that.data.data.categoryId == undefined) {
          that.getprice(num);
        } else {
          that.selectCategory(that.data.cateId, num)
        }
        break;
      default:
        //综合排序
        that.setData({
          categoryFilter: false,
          currentSortType: 'default',
          currentSortOrder: 'asc'
        });
        that.onLoad();
    }
  },

  //根据价格对列表进行排序
  getprice: function (num) {
    var that = this
    that.getNewData();
    var data = {
      isOnSale: 1,
      isHot: 1,
      orderById: num, //5表示按价格从低到高查询,6表示按价格从高到低查询
      goodsName: ''
    }
    that.setData({
      data: data
    })
    that.getGoodsList(data);
  },

  //选择分类类型查询商品列表
  selectCategory: function (e, num) {
    var that = this
    that.getNewData();
    if (num == undefined) {
      var currentId = e.target.dataset.categoryId;
      num = 5;
    } else {
      var currentId = that.data.cateId;
    }
    var data = {
      isOnSale: 1,   //必选 是上架
      isHot: 1,   // 必选 是热销商品
      categoryId: currentId, //二级商品分类id
      orderById: num,  //6表示按价格从高到低查询
      goodsName: ''
    }
    that.setData({
      data: data,
      categoryFilter: false,
      cateId: currentId
    })
    that.getGoodsList(data);
  },

  //  查询热门商品的一级分类
  getFrsCate: function () {
    var that = this
    var num = that.data.listnum
    var pageNum = that.data.startNum
    var data = {
      isHot: 1
    }
    wx.showLoading({
      title: '加载中',
    })
    util.requestNoToken(api.FrisCateList, data, "POST").then(function (res) {
      if (res.success) {
        wx.hideLoading();
        var lis = res.obj;
        lis.forEach(function (item, index, array) {
          array[index] = {
            id: item.id,
            name: item.categoryName,
            cate: 1,
          }
        });
        that.setData({
          filterCategory: lis
        })
      } else {
        wx.hideLoading();
        console.log("获取全部商品列表失败")
      }
    }).catch(function (res) {
      wx.hideLoading();
      console.log("获取数据失败！")
    })
  },

  //  查询热门商品的二级级分类
  getSeconCate: function (e) {
    var that = this
    var num = that.data.listnum
    var pageNum = that.data.startNum
    var data = {
      parentId: e.target.dataset.categoryId,
      isHot: 1
    }
    console.log('parentId' + e.target.dataset.categoryId)
    wx.showLoading({
      title: '加载中',
    })
    util.requestNoToken(api.SecoCateList, data, "POST").then(function (res) {
      if (res.success) {
        wx.hideLoading();
        var lis = res.obj;
        lis.forEach(function (item, index, array) {
          array[index] = {
            id: item.id,
            name: item.categoryName,
            cate: 2,
          }
        });
        that.setData({
          filterCategory: lis
        })
      } else {
        wx.hideLoading();
        console.log("获取全部商品列表失败")
      }
    }).catch(function (res) {
      wx.hideLoading();
      console.log("获取数据失败！")
    })
  },



  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },

})